package com.wisezone.studentmanager.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.wisezone.studentmanager.dao.UserDao;
import com.wisezone.studentmanager.model.User;

public class UserDaoImpl extends JdbcDaoSupport implements  UserDao {

	/**
	 * 数据库字段与对象属性映射
	 */
	public RowMapper<User> rowMapper = new RowMapper<User>() {
		@Override
		public User mapRow(ResultSet rs, int rowNum) throws SQLException {
			User user = new User();
			user.setUserName(rs.getString("username"));
			user.setPassWord(rs.getString("password"));
			user.setHeaderUri(rs.getString("fileuri"));
			return user;
		}
	};

	@Override
	public void addUser(User user) {
		String sql = "INSERT INTO user (username,password,fileuri)VALUES (?,?,?)";
		getJdbcTemplate().update(sql, user.getUserName(), user.getPassWord(), user.getHeaderUri());

	}

	@Override
	public User getUser(String username, String password) {
		String sql = "SELECT * FROM user WHERE username=? AND password=?";
		return getJdbcTemplate().queryForObject(sql, new Object[] { username, password }, rowMapper);
	}

	@Override
	public boolean checkUser(String username) {
		String sql = "SELECT * FROM user WHERE username=?";
		boolean isChceck = getJdbcTemplate().queryForObject(sql, new Object[] { username }, Boolean.class);
		return isChceck;
	}

	@Override
	public List<User> getUserLists() {
		String sql = "SELECT * FROM user";
		return getJdbcTemplate().query(sql, rowMapper);
	}

	@Override
	public List<User> getUserLists(int currentPageNo, int pageSize) {
		String sql = "SELECT * FROM user limit ?,?";
		return getJdbcTemplate().query(sql, new Object[] { currentPageNo, pageSize }, rowMapper);
	}

	@Override
	public int getTotalCount() {
		String sql = new String("SELECT count(*) FROM user where 1=1 ");
		return getJdbcTemplate().queryForObject(sql, Integer.class);
	}

	@Override
	public int getTotalCount(int sex, String name, int age) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<User> getUserLists(int sex, String userName, int age, int startIndex, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}
}
